﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddProgram.aspx.cs" company="">
//
// </copyright>
// <summary>
//   The add program.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AB.ServiceDirectory.Design.Programs
{
    using System;
    using System.Globalization;
    using System.Web.UI;

    using BO;
    using Entities;

    /// <summary>
    /// The add program.
    /// </summary>
    public partial class AddProgram : Page
    {
        #region Constants and Fields

        /// <summary>
        /// The _programme bo.
        /// </summary>
        private readonly ProgramBO _programmeBO = new ProgramBO();

        #endregion Constants and Fields

        #region Methods

        /// <summary>
        /// The button lookup_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void ButtonLookup_Click(object sender, EventArgs e)
        {
            this.ContactLookupPopup.Popup();
        }

        /// <summary>
        /// The button save_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            int? contactId = null;
            if (!string.IsNullOrEmpty(this.HiddenFieldManagerId.Value))
            {
                contactId = int.Parse(this.HiddenFieldManagerId.Value, CultureInfo.CurrentCulture);
            }

            string name = this.TextBoxName.Text;
            string description = this.TextBoxDescription.Text;

            if (this._programmeBO.CheckProgramName(name))
            {
                this.CheckLabel.Text = "Duplicate Programme Name";
            }
            else
            {
                var _program = new Program();
                _program.ProgramName = name;
                _program.Description = description;
                _program.ContactId = contactId;
                _program.IsActive = ButtonDeactivate.CommandName.Equals("active");
                bool checkInsert = _programmeBO.AddProgram(_program);

                if (checkInsert)
                {
                    this.Response.Redirect("~/Program/ProgramList.aspx");
                }
                else
                {
                    this.Response.Write("<script>alert('Insert Programme to database failed ! ')</script>");
                }
            }
        }

        /// <summary>
        /// The page_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #endregion Methods

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            ButtonDeactivate.CommandName = e.CommandName == ("active") ?
                "deactive" : "active";
            ButtonDeactivate.Text = e.CommandName == ("active") ?
                "Activate" : "Deactivate";
            LiteralIsActive.Text = ButtonDeactivate.CommandName.Equals("active") ? "Yes" : "No";

            ButtonDeactivate.OnClientClick = ButtonDeactivate.CommandName.Equals("active")
                                                 ? "return confirm('Do you want to make this Programme in-active?')"
                                                 : string.Empty;
        }

        /// <summary>
        /// Handles the Click event of the ButtonDeactivate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonDeactivate_Click(object sender, EventArgs e)
        {
        }
    }
}